package com.itasura.balking;

/**
 * @author sailor wang
 * @date 2018/9/6 下午1:00
 * @description
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class WashingMachine {

    private static final Logger LOGGER = LoggerFactory.getLogger(WashingMachine.class);

    private WashingMachineState washingMachineState;

    public WashingMachine(){
        this.washingMachineState = WashingMachineState.ENABLE;
    }

    public WashingMachineState getWashingMachineState(){
        return washingMachineState;
    }

    public void wash(){
        synchronized (this){
            if (washingMachineState == WashingMachineState.WASHING){
                LOGGER.error("该洗衣机正在使用");
                return;
            }
            washingMachineState = WashingMachineState.WASHING;
        }
        LOGGER.info("马上开洗 ",Thread.currentThread().getName());

        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        endofWashing();
    }

    public synchronized void endofWashing(){
        washingMachineState = WashingMachineState.ENABLE;
        LOGGER.info("洗完了 ",Thread.currentThread().getName());
    }
}